package com.mysoft.gfyx.marketdevelop.controller;

import com.mysoft.framework.modeling.dto.ListDataResult;
import com.mysoft.framework.rpc.annotation.PubAction;
import com.mysoft.framework.rpc.annotation.PubService;
import com.mysoft.framework.rpc.contants.RequestPrefix;
import com.mysoft.gfyx.common.dto.ListResult;
import com.mysoft.gfyx.common.rpc.opensearch.OpensearchClientFactoryService;
import com.mysoft.gfyx.common.rpc.opensearch.OpensearchClientService;
import com.mysoft.gfyx.common.rpc.opensearch.dto.OpenSearchSingleListResponseDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.*;
import com.mysoft.gfyx.marketdevelop.mip.dto.CheckMainDataResponseDto;
import com.mysoft.gfyx.marketdevelop.remoteService.dto.AddCustomersRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetInvestmentRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.SearchDeveloperRequestDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperContactService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperCooperatorService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.dto.DeveloperActivityRadarDto;
import com.mysoft.gfyx.marketdevelop.service.dto.DeveloperContactDto;
import io.swagger.annotations.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;
import java.util.Map;

@PubService(value = "/developerDataApi", prefix = RequestPrefix.API)
@Tag(name = "开发商资料-API")
public class DeveloperDataApiController {

    @Autowired
    private DeveloperService developerService;

    @Autowired
    private DeveloperCooperatorService developerCooperatorService;

    @Autowired
    private DeveloperContactService developerContactService;

    @Autowired
    private OpensearchClientFactoryService opensearchClientFactoryService;


    @PubAction(value = "/searchDeveloper", method = RequestMethod.POST)
    public Object searchDeveloper(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchDeveloper(dto);
    }

    @PubAction(value = "/searchEnterpriseName", method = RequestMethod.POST)
    public Object searchEnterpriseName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchEnterpriseName(dto.getKeyword());
    }

    @PubAction(value = "/searchOpportunityName", method = RequestMethod.POST)
    public Object searchOpportunityName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchOpportunityName(dto.getKeyword());
    }

    @PubAction(value = "/searchLandName", method = RequestMethod.POST)
    public Object searchLandName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchLandName(dto.getKeyword());
    }

    @PubAction(value = "/searchCommencePermitName", method = RequestMethod.POST)
    public Object searchCommencePermitName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchCommencePermitName(dto.getKeyword());
    }

    @PubAction(value = "/searchMohurdProjectName", method = RequestMethod.POST)
    public Object searchMohurdProject(@RequestBody SearchDeveloperRequestDto dto) {
        OpensearchClientService service = opensearchClientFactoryService.getOpensearchService();
        OpenSearchSingleListResponseDto res = service.searchMohurdProjectName(dto.getKeyword());
        return res.getData();
    }

    @PubAction(value = "/searchProjectName", method = RequestMethod.POST)
    public Object searchProjectName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchProjectName(dto.getKeyword());
    }

    @PubAction(value = "/searchWinBidName", method = RequestMethod.POST)
    public Object searchWinBidName(@RequestBody SearchDeveloperRequestDto dto) {
        return developerService.searchWinBidName(dto.getKeyword());
    }

    @PubAction(value = "/getOpportunityDetail", method = RequestMethod.POST)
    public Object getOpportunityDetail(@RequestBody GetOpportunityDetailRequestDto dto) {
        return developerService.getOpportunityDetail(dto.getId());
    }

    @PubAction(value = "/getWinBidDetail", method = RequestMethod.POST)
    public Object getWinBidDetail(@RequestBody GetOpportunityDetailRequestDto dto) {
        return developerCooperatorService.getWinBidById(dto.getId());
    }

    @PubAction(value = "/getDeveloperMetrics", method = RequestMethod.POST)
    public Object getDeveloperMetrics(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getMetrics(dto.getCreditCode());
    }

    @PubAction(value = "/getCertificates", method = RequestMethod.POST)
    public Object searchDeveloper(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getCertificates(dto);
    }

    @PubAction(value = "/getInvestments", method = RequestMethod.POST)
    public Object getInvestments(@RequestBody CreditCodeBaseRequestDto req) {
        GetInvestmentRequestDto dto = new GetInvestmentRequestDto();
        dto.setCredit_code(req.getCreditCode());
        return developerService.getInvestment(dto);
    }

    @PubAction(value = "/getDeveloperMainData", method = RequestMethod.POST)
    public Object getDeveloperMainData(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperMainDataAndSummary(dto.getCreditCode());
    }

    @PubAction(value = "/getDeveloperDetailRiskCount", method = RequestMethod.POST)
    public Object getDeveloperDetailRiskCount(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperDetailRiskCount(dto.getCreditCode());
    }

    @PubAction(value = "/addCustomers", method = RequestMethod.POST)
    public Object addCustomers(@RequestBody AddCustomersRequestDto dto) {
        return developerService.addCustomers(dto);
    }

    @PubAction(value = "/getOpenedTradingData", method = RequestMethod.POST)
    public Object getOpenedTradingData(@RequestBody GetOpenedTradingDataRequestDto dto) {
        return developerService.getOpenedTradingData(dto.getCreditCode(), dto.getFilters(), dto.getPager());
    }

    @PubAction(value = "/getProcurementRequirementData", method = RequestMethod.POST)
    public Object getProcurementRequirementData(@RequestBody GetProcurementRequirementDataRequestDto dto) {
        return developerService.getProcurementRequirementData(dto.getCreditCode(), dto.getFilters(), dto.getPager());
    }

    @PubAction(value = "/getContacts", method = RequestMethod.POST)
    @ApiOperation(value = "/getContacts", notes = "获取当前开发商的联系人列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "开发商ID", paramType = "query", dataType = "String", required = true), @ApiImplicitParam(name = "pager", value = "分页", paramType = "query", dataType = "PagerDto", required = true)})
    public ListResult<DeveloperContactDto> getContacts(@RequestBody GetContactRequestDto dto) {
        return developerContactService.getDeveloperContact(dto.getId(), dto.getPager());
    }

    @PubAction(value = "/getFeeds", method = RequestMethod.POST)
    @ApiOperation(value = "/getFeeds", notes = "获取当前开发商的动态列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "organizing_code", value = "开发商信用代码", paramType = "query", dataType = "String", required = true)})
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = ListDataResult.class)})
    public ListResult<Map<String, Object>> getFeeds(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getDeveloperFeeds(dto.getCreditCode());
    }

    @PubAction(value = "/getActivityRadar", method = RequestMethod.POST)
    @ApiOperation(value = "/getActivityRadar", notes = "获取当前开发商的活跃度雷达图")
    @ApiImplicitParams({@ApiImplicitParam(name = "organizing_code", value = "开发商信用代码", paramType = "query", dataType = "String", required = true)})
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = ListDataResult.class)})
    public DeveloperActivityRadarDto getActivityRadar(@RequestBody CreditCodeBaseRequestDto dto) {
        return developerService.getActivityRadar(dto.getCreditCode(), null);
    }

    @PubAction(value = "/checkDeveloperMainData", method = RequestMethod.POST)
    public List<CheckMainDataResponseDto> checkDeveloperMainData(@RequestBody CheckDeveloperMainDataRequestDto dto) {
        return developerService.getDeveloperMainDataMultiName(dto.getDeveloperName());
    }

    @PubAction(value = "/applyDeveloperMainData", method = RequestMethod.POST)
    @ApiOperation(value = "/applyDeveloperMainData", notes = "申请反哺开发商主档案接口")
    public Object applyDeveloperMainData(@RequestBody ApplyDeveloperMainDataDto reqDto) {
        if (reqDto.getDevelopers() != null && reqDto.getDevelopers().size() > 0) {
            return developerService.applyDeveloperMainData(reqDto);
        } else if (reqDto.getSupplier() != null && reqDto.getSupplier().size() > 0) {
            return developerService.applySupplierMainData(reqDto);
        } else {
            return null;
        }
    }

    @PubAction(value = "/getDeveloperScoreDetail")
    public Object getDeveloperScoreDetail(@RequestBody GetDeveloperScoreDetailRequest dto) {
        return developerService.getDeveloperScoreDetail(dto.getOrganizing_code());
    }
}
